#!/bin/bash

# set one location
# ONE_LOCATION=

##################### [ checking opennebula installation ] ####################

echo
if [ -z "${ONE_LOCATION}" ]; then
	LIB_LOCATION="/usr/lib/one"
	ETC_LOCATION="/etc/one"
else
	LIB_LOCATION="${ONE_LOCATION}/lib"
	ETC_LOCATION="${ONE_LOCATION}/etc"  
fi
SRC_DIR=$(dirname $0)/src

# check dirs
if [ ! -d "$LIB_LOCATION" ]; then
	echo "OpenNebula installation not found"
	exit 1
fi

IM_DIR="${LIB_LOCATION}/remotes/im/vbox.d"
VMM_DIR="${LIB_LOCATION}/remotes/vmm/vbox"

echo "OpenNebula installation found:"
echo "......................LIB LOCATION: $LIB_LOCATION"
echo ".......VMM REMOTE SCRIPTS LOCATION: $VMM_DIR"
echo "........IM REMOTE SCRIPTS LOCATION: $IM_DIR"




########################## [ installing vbox driver ] #########################


if [ ! -w "${ETC_LOCATION}/vmm_ssh" ]; then
	echo "You must have write permissions for ${ETC_LOCATION}/vmm_ssh"
	exit 1
fi

mkdir "$IM_DIR" || {
	echo "unable to create "$IM_DIR" dir. Check permissions"
	exit 1
}
mkdir "$VMM_DIR" || {
	echo "unable to create "$VMM_DIR" dir. Check permissions"
	exit 1 
}


echo "Copying files ..."

# copying IM files ...
cp "${LIB_LOCATION}/remotes/im/xen.d/architecture.sh" "$IM_DIR"
cp "${LIB_LOCATION}/remotes/im/xen.d/name.sh" "$IM_DIR"
cp "${LIB_LOCATION}/remotes/im/xen.d/cpu.sh" "$IM_DIR"
cp -R $SRC_DIR/im/* "$IM_DIR"

# copying VMM files ...
cp -R ${SRC_DIR}/vmm/* "$VMM_DIR"


cp "$SRC_DIR/vmm_ssh_vbox.conf" "${ETC_LOCATION}/vmm_ssh"


####################### [ echoing guideline for users ] #######################

echo
echo "Installation complete!

Configure ${ETC_LOCATION}/oned.conf like this:
e.g.

"
cat sample.oned.conf
echo -e "\nThen add hosts using those drivers.
e.g.
$ onehost create <HOST> im_vbox vmm_vbox tm_nfs
"

